package com.example.smart_campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.smart_campus.common.Res;

import com.example.smart_campus.pojo.dto.teachers.TeacherQuery;
import com.example.smart_campus.pojo.dto.teachers.TeacherUpdate;
import com.example.smart_campus.pojo.dto.teachers.TeachersTable;
import com.example.smart_campus.pojo.entity.Teachers;
import com.example.smart_campus.dao.TeachersMapper;
import com.example.smart_campus.service.TeachersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 教师表，存储教师的基本信息 服务实现类
 * </p>
 *
 * @author liyv
 * @since 2024-10-19
 */
@Service
public class TeachersServiceImpl extends ServiceImpl<TeachersMapper, Teachers> implements TeachersService {
    @Autowired
    private TeachersMapper teachersMapper;

    @Override
    public Res importByEList(List<TeachersTable> tableList) {

        List<Teachers> TeachersList = new ArrayList<>();
        tableList.forEach(item->{
            Teachers teacher = new Teachers(item);
            StringBuilder courseBuilder = new StringBuilder();
            item.getCourses().forEach((c -> {
                if (courseBuilder.length() > 0) {
                    courseBuilder.append(", ");
                }
                courseBuilder.append(c);
            }));
            String course = courseBuilder.toString();
            teacher.setCourses(course);
            TeachersList.add(teacher);
        });
        int size = TeachersList.size();

        if(teachersMapper.insertBatchSomeColumn(TeachersList) == size) {
            return Res.ok() ;
        }

        return Res.failed() ;
    }

    @Override
    public Res<List<Teachers>> getAllTeachers() {
        return Res.ok(teachersMapper.selectList(Wrappers.emptyWrapper()));
    }

    @Override
    public Res<List<Teachers>> getByQuery(TeacherQuery query) {
        QueryWrapper<Teachers> queryWrapper = new QueryWrapper<>() ;
        queryWrapper.like(!query.getSearch().isEmpty(),"name",query.getSearch())
                .or().like(!query.getSearch().isEmpty(),"teacher_no",query.getSearch());

        return Res.ok(teachersMapper.selectList(queryWrapper));
    }

    @Override
    @Transactional
    public Res delById(String tid) {
        int i = teachersMapper.deleteById(tid);
        if (i > 0)
            return Res.ok();
        return Res.failed();
    }

    @Override
    @Transactional
    public Res updateTeacher(TeacherUpdate update) {
        Teachers t = teachersMapper.selectById(update.getTeacherId()) ;
        if (t == null)
            return Res.failed("not found");
        UpdateWrapper<Teachers> wrapper = new UpdateWrapper<>();
        StringBuilder courseBuilder = new StringBuilder();
        update.getCourses().forEach((c -> {
            if (courseBuilder.length() > 0) {
                courseBuilder.append(", ");
            }
            courseBuilder.append(c);
        }));
        String course = courseBuilder.toString();
        wrapper.eq("teacher_id",update.getTeacherId())
                .set(!update.getTeacherNo().isEmpty(),"teacher_no",update.getTeacherNo())
                .set(!update.getName().isEmpty(),"name",update.getName())
                .set(!update.getMajor().isEmpty(),"major",update.getMajor())
                .set(!update.getPhone().isEmpty(),"phone",update.getPhone())
                .set("courses",course) ;
        int r = teachersMapper.update(t,wrapper);
        if (r > 0) return Res.ok() ;
        return Res.failed();
    }
}
